<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <style>
    .list {
      padding: 10px;
    }
    .item {
      border: 1px solid greenyellow;
      margin: 10px 0px;
    }
  </style>
  <body>
    <div id="root"></div>

    <script src="../lib/react.js"></script>
    <script src="../lib/react-dom.js"></script>
    <script src="../lib/babel.js"></script>

    <script type="text/babel">
      // let message = "Hello World"
      // function btnClick(){
      //     console.log("btnClick");
      //     message = "Hello react"
      //     rootRender()
      // }
      // const root = ReactDOM.createRoot(document.querySelector("#root"))
      // function rootRender(){
      //     root.render(
      //     <div>
      //         <h2>{message}</h2>
      //         <button onClick={btnClick}> 修改</button>
      //     </div>
      //     )
      // }
      // rootRender()
      // 使用组件进行重构代码
      // 类组件和函数式组件

      class App extends React.Component {
        // 组件数据
        constructor() {
          super();
          this.state = {
            message: "hello world",
            students: [
              { id: 111, name: "aaa", score: 199 },
              { id: 112, name: "bbb", score: 97 },
              { id: 113, name: "ccc", score: 100 },
              { id: 114, name: "ddd", score: 180 },
            ],
          };

          // 对需要绑定的方法，提前绑定好this
        }
        // 组件方法 (实例方法)

        // 渲染内容  render方法
        render() {
          const { students } = this.state;
          //   分数大于100
          const filterStudents = students.filter(
            (value, index) => value.score >= 100
          );
          //   分数大于100  只展示两个人的信息
          const sliceStudents = filterStudents.slice(0, 2);
          // 排序
          sliceStudents.sort((a, b) => {
            return a.score - b.score;
          });
          return (
            <div>
              <h2>学生列表数据</h2>
              <div className="list">
                {sliceStudents.map((item) => {
                  return (
                    <div className="item" key={item.id}>
                      <h2>学号: {item.id}</h2>
                      <h3>姓名: {item.name}</h3>
                      <h1>分数: {item.score}</h1>
                    </div>
                  );
                })}
              </div>
            </div>
          );
        }
      }

      // 将组件渲染到界面上
      const root = ReactDOM.createRoot(document.querySelector("#root"));
      root.render(<App />);
    </script>
  </body>
</html>
